
osname = 'linux'

import os.path

def join(*p):
    return os.path.normpath(os.path.join(*p))


def list2path(p):
    if type(p) == list or type(p) == tuple:
        p = os.path.join(*p)
    return p

def path2list(p):
    if type(p) == str or type(p) == unicode:
        p = [p]
    return p

def genonepath(dir, ext):
    dir = list2path(dir)
    ext = '?.' + ext.lstrip('.')
    return join(dir, ext)

def genpath(dirlist, extlist):
    dirlist = path2list(dirlist)
    extlist = path2list(extlist)
    pathlist = []
    for dir in dirlist:
        for ext in extlist:
            p = genonepath(dir, ext)
            pathlist.append(p)
    return ';'.join(pathlist)


def initenv(workdir = None):
    if not workdir:
        workdir = os.path.normpath(os.path.abspath(os.getcwd()))
    root = join(workdir, '..', 'linux')
    lua_path = genpath([[root,'share']], 'lua')
    lua_cpath = genpath([[root,'lib']], 'so')
    if os.environ.has_key('LUA_PATH'):
        os.environ['LUA_PATH'] = os.environ['LUA_PATH'] + ';' + lua_path
    else:
        os.environ['LUA_PATH'] = '?.lua' + ';' + lua_path
    if os.environ.has_key('LUA_CPATH'):
        os.environ['LUA_CPATH'] = os.environ['LUA_CPATH'] + ';' + lua_cpath
    else:
        os.environ['LUA_CPATH'] = '?.so' + ';' + lua_cpath
    pass

def cmddebug(filepath):
    filename = os.path.basename(filepath)
    binpath = join(os.getcwd(), r'../linux/bin/lua')
    srcdir = join(os.getcwd(), r'../src')
    srcpath = os.path.join(srcdir, 'toydebugger.lua')

    #os.spawnl(os.P_NOWAIT, binpath, 'lua.exe', srcpath, filename)
    #os.system('start='+binpath+' '+srcpath+' '+filename)
    #os.system('start=pause-run '+binpath+' '+srcpath+' '+filename)
    cmd = binpath+' -l bufutil -e bufutil.nobuf() '+srcpath+' '+filename

    return cmd


def cmdrun(filepath):
    filename = os.path.basename(filepath)
    binpath = 'lua'
    binpath = '/home/pleiades/tmp/lualocal/bin/lua'
    cmd = binpath+' -l bufutil -e bufutil.nobuf() '+filename
    return cmd


